﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DAL.Interface;
using Model;

namespace DAL.Dao
{
    public class UserInfoDaoimpl : Base, IUserDao
    {

        public int ExistNum { get { return DbEntity.expressDBEntities.Manager.Count(); } private set { } }
        public UserInfoDaoimpl()
        {
            DbEntity.CheckConn();
        }
        public bool Update(int id, UserInfo newitem)
        {

            DbEntity.CheckConn();
            UserInfo item = (from i in DbEntity.expressDBEntities.UserInfo
                                where i.ID == id
                                select i).ToList().FirstOrDefault();
            if (item == null)
            {
                return false;
            }
            //DbEntityEntry<Bag> entry = DbEntity.expressDBEntities.Entry<Bag>(newitem);
            //3.设置该对象为被修改过
            //entry.State = EntityState.Unchanged;
            //4.设置该对象的 uName属性为修改状态，同时 entry.State 被修改为 Modified 状态
            item.UserName = newitem.UserName;
            //item.UserPass = newitem.UserPass;
            item.UpdateTime = newitem.UpdateTime;
            item.TrueName = newitem.TrueName;
            item.Phone = newitem.Phone;
            item.ContactAddress = newitem.ContactAddress;
            item.Email = newitem.Email;
            item.IDCard = newitem.IDCard;
            //item.UpdateTime = newitem.UpdateTime;
            //item.AddTime = newitem.AddTime;


            DbEntity.expressDBEntities.SaveChanges();

            return true;
        }



        public IList<UserInfo> GetList()
        {
            DbEntity.CheckConn();
            return (from i in DbEntity.expressDBEntities.UserInfo orderby i.ID select i).ToList();
        }


        public (int,IList<UserInfo>) GetList(int rows , int page)
        {
            DbEntity.CheckConn();
            var rs = (from i in DbEntity.expressDBEntities.UserInfo orderby i.ID select i).ToList();
            return (GetTotalPages(rs.Count,rows),rs.Skip((page-1)*rows).Take(rows).ToList());
        }

        public UserInfo Login(string username, string userpass)
        {
            DbEntity.CheckConn();
            UserInfo rs = null;
            try
            {
               rs =  (from i in DbEntity.expressDBEntities.UserInfo
                        where i.UserName.Trim().Equals(username) && i.UserPass.Trim().Equals(userpass)
                        select i).ToList().FirstOrDefault();
            }
            catch (Exception)
            {

                return null;
            }

            return rs;
        }


        public bool Delete(int id)
        {
            DbEntity.CheckConn();
            UserInfo user = DbEntity.expressDBEntities.UserInfo.Find(id);
            if (user == null) return false;
            DbEntity.expressDBEntities.UserInfo.Remove(user);
            DbEntity.expressDBEntities.SaveChanges();
            return true;
        }
        public UserInfo Find(int id)
        {
            DbEntity.CheckConn();
            return DbEntity.expressDBEntities.UserInfo.Find(id);
           
        }

        public bool Add(UserInfo userInfo)
        {
            try
            {
                DbEntity.CheckConn();
                DbEntity.expressDBEntities.UserInfo.Add(userInfo);
                DbEntity.expressDBEntities.SaveChanges();
            }
            catch (Exception)
            {

                return false;
            }
       
            return true;
        }
        public IList<UserInfo> GetUserInfo(int id)
        {
            DbEntity.CheckConn();
            //使用Lambda表达式查询数据
            var info = (from i in DbEntity.expressDBEntities.UserInfo
                        where i.ID==id
                        select i).ToList();

            return info;

        }
        public IList<UserInfo> GetUserInfo(string username)
        {
            DbEntity.CheckConn();
            //使用Lambda表达式查询数据
            var info = (from i in DbEntity.expressDBEntities.UserInfo
                        where i.UserName .Equals(username)
                        select i).ToList();

 
            return info;

        }

        public void Dispose()
        {
            if (DbEntity.expressDBEntities != null) DbEntity.expressDBEntities.Database.Connection.Close();
        }

    }
}
